<template>
  <div class="flex flex-col h-full">
    <SearchContainer @search="getData" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="form-style-flex" :model="formState" :label-col="{ style: { width: '70px' } }">
        <a-form-item name="dateRange" label="收费日期" class="w-80 mr-5">
          <a-range-picker v-model:value="formState['dateRange']" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
        </a-form-item>
        <a-form-item name="pyjm" label="" class="w-64">
          <a-input v-model:value="formState.pyjm" placeholder="姓名/助记码/卡号/手机号/身份证号" />
        </a-form-item>
        <a-form-item name="pjhm" class="w-64 ml-5">
          <a-input v-model:value="formState.pjhm" placeholder="票据号" />
        </a-form-item>
        <a-form-item name="isTf" class="w-64 ml-5">
          <a-radio-group v-model:value="formState.isTf">
            <a-radio value="">全部</a-radio>
            <a-radio value="SF">收费</a-radio>
            <a-radio value="TF">退费</a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item name="ismd" class="w-64 ml-5">
          <a-checkbox v-model:checked="formState.ismd" class="">免单数据</a-checkbox>
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2 flex-1">
      <div class="flex justify-end mb-2">
        <BaseExport type="门诊" name="收费检索" code="001055" :search-params="formState">导出</BaseExport>
      </div>
      <BaseTable
        fill
        :loading="loading"
        column-code="00001171"
        :extra-height="50"
        :scroll="{ x: 1800 }"
        :data-source="tableData"
        bordered
        :pagination="pagination"
      >
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <!-- <template v-if="column.dataIndex === 'ssje'">
            {{ record.ysje }}
          </template> -->
          <template v-if="column.dataIndex === 'action'">
            <a-button type="link" size="small" @click="onHandleEdit(record)"> 明细 </a-button>
            <a-divider type="vertical" />
            <a-button type="link" size="small" @click="onPrint(record)"> 补打收据 </a-button>
          </template>
        </template>
        <template #summary>
          <a-table-summary fixed>
            <a-table-summary-row>
              <template v-for="column in columnsData" :key="column.dataIndex">
                <a-table-summary-cell v-if="column.dataIndex === 'xh'"> 合计 </a-table-summary-cell>
                <a-table-summary-cell v-else-if="!column.isHide">
                  <div :class="bottomTotal[column.dataIndex] ? 'text-right' : 'text-center'">{{ bottomTotal[column.dataIndex] || '-' }}</div>
                </a-table-summary-cell>
              </template>
            </a-table-summary-row>
          </a-table-summary>
        </template>
      </BaseTable>
    </a-card>
    <Edit v-model:open="editOpen" :row-data="rowData" @success="onSuccess" />
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import dayjs from 'dayjs'
import { message } from 'ant-design-vue'
import SearchContainer from '@/components/SearchContainer'
import { post } from '@/utils/request'
import Edit from './Edit.vue'

const rowData = ref({})
const editOpen = ref(false)
const formRef = ref(null)
const formState = ref({
  dateRange: [dayjs().format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')],
  type: '1',
  isTf: '',
  ismd: false
})
const { loading, tableData, pagination, getData } = useTable({
  url: '/outp/oupCharge/getTollRetrievalListSg',
  params: formState.value,
  formatParams: (params) => {
    getTotal({
      ...params,
      ismd: params.ismd ? 'MD' : ''
    })
    return {
      ...params,
      ismd: params.ismd ? 'MD' : ''
    }
  }
})
const bottomTotal = ref({})
const getTotal = async (params) => {
  const { code, data } = await post('/outp/oupCharge/getTollRetrievalHJ', params)
  if (code !== 0) return message.error(code)
  bottomTotal.value = data || {}
}

const onHandleEdit = (row) => {
  rowData.value = { ...row }
  editOpen.value = true
}

//获取表头
const columnsData = ref([])
const getTableColumns = async () => {
  const { code, data } = await post('/common/util/getTableMappingCloumnList', { id: '00001171' })
  if (code !== 0) return message.error(code)
  columnsData.value = data
}

const onPrint = async (row) => {
  const { code, msg, data } = await post('/outp/oupCharge/PrintMedicareCharges', { jzbh: row.jzbh, pjhm: row.pjhm, type: '1' })
  if (code !== 0) return message.error(msg)
  window.openExe?.rpt(data) //调用winform打印
}

const onSuccess = () => {
  editOpen.value = false
  getData()
}
onMounted(() => {
  getData()
  getTableColumns()
})
</script>
<style lang="less" scoped>
.ant-picker {
  width: 100%;
}
</style>
